Resumen

Row

Cantidad de proyectos

160795

Total presupuesto inicial

64797.56

Total presupuesto vigente

83055.65

Total ejecutado

49224.46

% ejecutado

Row

Presupuestado vs Ejecutado

Row

Cantidad de proyectos por departamento

Porcentaje de proyectos por departamento

Sector

Row

Numero de Proyectos por tipo de Sector

% ejecucion por tipo de Sector

Row

Barras

Area

Tabla

Departamento

Row

Numero de Proyectos por Departemento

% ejecucion por departamento

Row

Barras

Area

Tabla

Region

Row

Numero de Proyectos por region

% ejecucion por Region

Row

Barras

Densidad

Tabla

Tipo de entidad

Row

Numero de Proyectos por tipo de entidad

% ejecucion por tipo de entidad

Row

Barras

Densidad

Tabla

Financiamiento

Row

Numero de Proyectos por financiamiento

% ejecucion por financiamiento

Row

Barras

Densidad

Tabla

Grafico Dinamico

Informe

---
title: "Inversion publica 2006-2022"
author: "Ada Cueto - Alexander Sucre"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
    theme:
      version: 4
      base_font:
        google: Sen
      code_font:
        google: JetBrains Mono
      bootswatch: darkly
---

```{r setup, include=FALSE}
rm(list = ls())
library("flexdashboard")
pacman::p_load(
  tidyverse,
  sparklyr,
  readxl,
  dplyr,
  ggplot2,
  GGally,
  ggthemes,
  plotly,
  DataExplorer,
  textshape,
  reticulate,
  knitr,
  DT
)
sc <- spark_connect(master = "local")
proy <- spark_read_csv(sc, name = "proyecto", path = "../_data/Proyectos.csv",memory = F)
proy <- proy %>% 
  mutate(presup=ifelse(!is.na(presupuesto_vigente) & presupuesto_vigente > 0,presupuesto_vigente,Presupuesto_inicial))
```

# Resumen {data-icon="fa-home"}

Row
-----------------------------------------------------------------------

### Cantidad de proyectos {.value-box}

```{r}
tot_proy <- proy %>% count() %>% rename(total=n) %>% collect()
valueBox(tot_proy, icon = "fa-database",color = "info")
```

### Total presupuesto inicial {.value-box}

```{r}
tot_pres <- proy %>% summarise(total=round(sum(Presupuesto_inicial),2)) %>% collect()
valueBox(tot_pres, icon = "fa-dollar-sign",color = "primary")
```

### Total presupuesto vigente {.value-box}

```{r}
tot_vig <- proy %>% 
  summarize(total = round(sum(presup),2)) %>% collect()
valueBox(tot_vig, icon = "fa-chart-line",color = "danger")
```

### Total ejecutado {.value-box}

```{r}
tot_ejec <- proy %>% summarise(total=round(sum(presupuesto_ejecutado,na.rm = TRUE),2)) %>% collect()
valueBox(tot_ejec, icon = "fa-money-bill-alt",color = "success")
```

### % ejecutado 

```{r}
por_ejec <- round(tot_ejec/tot_vig*100)
gauge(por_ejec$total, min = 0, max = 100, symbol = "%",
      gaugeSectors(
        success = c(0,por_ejec$total)
      ))
```

Row
-----------------------------------------------------------------------

### Presupuestado vs Ejecutado

```{r}
gest <- proy %>% 
  select(gestion,inicial=Presupuesto_inicial,vigente=presup,ejecutado=presupuesto_ejecutado) %>% 
  group_by(gestion) %>% 
  summarise(inicial=sum(inicial),vigente=sum(vigente),ejecutado=sum(ejecutado)) %>% 
  arrange(gestion) %>% 
  collect()

fig <- plot_ly(gest, x = ~gestion, y = ~inicial, type = 'scatter', mode = 'lines', fill = 'tozeroy', name = 'Inicial',fillcolor = 'primary')
fig <- fig %>% add_trace(y = ~vigente, name = 'Vigente',fillcolor = 'danger')
fig <- fig %>% add_trace(y = ~ejecutado, name = 'Ejecutado',fillcolor = 'info')

fig <- fig %>% layout(
  xaxis = list(title = 'Gestión'),
  yaxis = list(title = 'Monto en millones de dolares'),
  title = 'Comparación de presupuesto inicial, vigente y ejecutado por gestión'
)

fig
```

Row
-----------------------------------------------------------------------

### Cantidad de proyectos por departamento

```{r}
dep_n <- proy %>% 
  count(depto) %>% 
  rename(Departamento=depto,Cantidad=n) %>% 
  collect()
datatable(dep_n,options = list(paging=FALSE,searching=FALSE,info=FALSE))
```

### Porcentaje de proyectos por departamento

```{r}
dep_p <- proy %>% 
  count(depto) %>% 
  mutate(porcentaje = n/sum(n)*100) %>% 
  select(Departamento=depto,Porcentaje=porcentaje) %>%  
  collect()
plot_ly(dep_p,labels=~Departamento,values=~Porcentaje,type = "pie") %>% 
  layout(title="Proyectos por departamento",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
```

# Sector {data-navmenu="Analisis" data-icon="fa-business-time"}

```{r}
sector_tipo <- proy %>% 
  group_by(gestion,tipo_sector) %>% 
  summarise(tot_proy = count(),presup=sum(presup,na.rm = TRUE),ejec=sum(presupuesto_ejecutado,na.rm = TRUE))
```

Row {data-height=350}
-----------------------------------------------------------------------

### Numero de Proyectos por tipo de Sector

```{r}
num_proy <- sector_tipo %>% 
  group_by(tipo_sector) %>%
  summarise(Cantidad = sum(tot_proy)) %>% 
  collect()

proy_reg <- ggplot(num_proy, aes(x = tipo_sector, y = Cantidad, fill = tipo_sector)) +
  geom_bar(stat = "identity",position = "stack") +
  labs(title = "Proyectos por tipo de sector", x = "tipo_sector", y = "Cantidad") +
  coord_flip()+
  theme_classic()
ggplotly(proy_reg)

```


### % ejecucion por tipo de Sector

```{r}
plot_ly(num_proy,labels=~tipo_sector,values=~Cantidad,type = "pie", hole=0.4 ) %>% 
  layout(title="Proyectos por tipo de sector",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

```

Row {data-height=650, .tabset .tabset-fade}
-----------------------------------------------------------------------

### Barras 

```{r}
barra_reg <- ggplot(sector_tipo, aes(x = gestion, y = ejec, fill = tipo_sector)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Ejecución por tipo de sector y Gestión",
       x = "Gestión", y = "Ejecución") +
  theme_minimal()
ggplotly(barra_reg)
```

### Area 

```{r}
area_reg <- ggplot(sector_tipo, aes(x = ejec)) +
  geom_density(aes(fill = gestion), alpha = 0.5) +
  labs(title = "Ejecución por Gestión (Densidad)",
       x = "Ejecución") +
  theme_minimal()
ggplotly(area_reg)
```

### Tabla 

```{r}
datatable(sector_tipo %>% collect())
```

# Departamento {data-navmenu="Analisis" data-icon="fa-city"}

```{r}
departamento <- proy %>% 
  group_by(gestion,depto) %>% 
  summarise(tot_proy = count(),presup=sum(presup,na.rm = TRUE),ejec=sum(presupuesto_ejecutado,na.rm = TRUE))
```

Row {data-height=350}
-----------------------------------------------------------------------

### Numero de Proyectos por Departemento

```{r}
bar<-ggplot(data=dep_n, aes(x=Departamento, y=Cantidad, fill=Departamento))+
  geom_bar(stat = "identity")

ggplotly(bar)


```


### % ejecucion por departamento

```{r}
dep_pe <- departamento %>% 
  group_by(Departamento = depto) %>% 
  summarise(Porcentaje = round(sum(ejec)/sum(presup)*100),2) %>% 
  arrange(Departamento) %>% 
  collect()

fig <- plot_ly(dep_pe, x = ~Departamento, y = ~Porcentaje, type = 'scatter', mode = 'lines', fill = 'tozeroy', name = 'Inicial',fillcolor = 'primary')

fig <- fig %>% layout(
  xaxis = list(title = 'Departamento'),
  yaxis = list(title = 'Porsentaje de ejecucion'),
  title = 'Porcentaje de ejecucion por departemento'
)

fig

plot_ly(dep_pe,labels=~Departamento,values=~Porcentaje,type = "pie" ) %>% 
  layout(title="% Ejecucion por Departamento",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
```

Row {data-height=650, .tabset .tabset-fade}
-----------------------------------------------------------------------

### Barras 

```{r}
dep_vs<-departamento %>%
  group_by(depto) %>% 
  summarise(Presupuestado = sum(presup),Ejecutado = sum(ejec)) %>% 
  arrange(depto) %>% 
  collect()

dep_vs1 <- pivot_longer(dep_vs, cols = c(Presupuestado, Ejecutado), names_to = "Tipo", values_to = "Monto")

bar_dep <- ggplot(dep_vs1, aes(x = depto, y = Monto, fill = Tipo))+
  geom_bar(stat = "identity",position = "dodge")+
  labs(title = "Presupuesto vs. Ejecución por Departamento", x = "Departamento", y = "Monto")+
  theme_minimal()+
  theme(axis.text = element_text(angle = 45,hjust = 1))
ggplotly(bar_dep)
```

### Area 

```{r}

fig <- plot_ly(dep_vs, x = ~depto, y = ~Presupuestado, type = 'scatter', mode = 'lines', fill = 'tozeroy', name = 'Presupuestado',fillcolor = 'primary')
fig <- fig %>% add_trace(y = ~Ejecutado, name = 'Ejecutado',fillcolor = 'danger')

fig <- fig %>% layout(
  xaxis = list(title = 'Departamento'),
  yaxis = list(title = 'Monto en millones de dolares'),
  title = 'Comparación de presupuesto vs ejecutado por gestión'
)

fig
```

### Tabla 

```{r}
datatable(departamento %>% collect())
```

# Region {data-navmenu="Analisis" data-icon="fa-map-marked"}

```{r}
region_d <- proy %>% 
  group_by(gestion,region) %>% 
  summarise(tot_proy = count(),presup=sum(presup,na.rm = TRUE),ejec=sum(presupuesto_ejecutado,na.rm = TRUE))
```

Row {data-height=350}
-----------------------------------------------------------------------

### Numero de Proyectos por region

```{r}
num_proy <- region_d %>% 
  group_by(region) %>%
  summarise(Cantidad = sum(tot_proy)) %>% 
  rename(Region = region) %>% 
  collect()

proy_reg <- ggplot(num_proy, aes(x = Region, y = Cantidad, fill = Region)) +
  geom_bar(stat = "identity",position = "stack") +
  labs(title = "Proyectos por region", x = "Region", y = "Cantidad") +
  coord_flip()+
  theme_classic()
ggplotly(proy_reg)

```


### % ejecucion por Region

```{r}
plot_ly(num_proy,labels=~Region,values=~Cantidad,type = "pie", hole=0.4 ) %>% 
  layout(title="Proyectos por Región",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

```

Row {data-height=650, .tabset .tabset-fade}
-----------------------------------------------------------------------

### Barras 

```{r}
barra_reg <- ggplot(region_d, aes(x = gestion, y = presup + ejec, fill = region)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Presupuesto y Ejecución por Región",
       x = "Gestión", y = "Ejecución") +
  theme_minimal()
ggplotly(barra_reg)
```

### Densidad 

```{r}
area_reg <- ggplot(region_d, aes(x = ejec)) +
  geom_density(aes(fill = gestion), alpha = 0.5) +
  labs(title = "Presupuesto y Ejecución por Región",
       x = "Ejecución") +
  theme_minimal()
ggplotly(area_reg)
```

### Tabla 

```{r}
datatable(region_d %>% collect())
```

# Tipo de entidad {data-navmenu="Analisis" data-icon="fa-suit-case"}

```{r}
tipo_entidad_r <- proy %>% 
  group_by(gestion,tipo_entidad) %>% 
  summarise(tot_proy = count(),presup=sum(presup,na.rm = TRUE),ejec=sum(presupuesto_ejecutado,na.rm = TRUE))
```

Row {data-height=350}
-----------------------------------------------------------------------

### Numero de Proyectos por tipo de entidad 

```{r}
num_proy <- tipo_entidad_r %>% 
  group_by(tipo_entidad) %>%
  summarise(Cantidad = sum(tot_proy)) %>% 
  arrange(desc(Cantidad)) %>% 
  head(15) %>% 
  collect()

proy_reg <- ggplot(num_proy, aes(x = tipo_entidad, y = Cantidad, fill = tipo_entidad)) +
  geom_bar(stat = "identity",position = "stack") +
  labs(title = "Proyectos por tipo de entidad", x = "Tipo entidad", y = "Cantidad") +
  coord_flip()+
  theme_classic()
ggplotly(proy_reg) %>% layout(showlegend = FALSE)

```


### % ejecucion por tipo de entidad

```{r}
num_proy_1 <- num_proy %>% head(10)
plot_ly(num_proy_1,labels=~tipo_entidad,values=~Cantidad,type = "pie", hole=0.4 ) %>% 
  layout(title="Proyectos por tipo de entidad",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

```

Row {data-height=650, .tabset .tabset-fade}
-----------------------------------------------------------------------

### Barras 

```{r}
barra_reg <- ggplot(tipo_entidad_r, aes(x = gestion, y = ejec, fill = tipo_entidad)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Ejecución por tipo de entidad",
       x = "Gestión", y = "Ejecución") +
  theme_minimal()
ggplotly(barra_reg)
```

### Densidad 

```{r}
area_reg <- ggplot(tipo_entidad_r, aes(x = ejec)) +
  geom_density(aes(fill = tipo_entidad), alpha = 0.5) +
  labs(title = "Ejecución por tipo de entidad",
       x = "Ejecución") +
  theme_minimal()
ggplotly(area_reg)
```

### Tabla 

```{r}
datatable(tipo_entidad_r %>% collect())
```

# Financiamiento {data-navmenu="Analisis" data-icon="fa-money-bill-wave"}

```{r}
financiamiento <- proy %>% 
  group_by(gestion,tipo_financiamiento) %>% 
  summarise(tot_proy = count(),presup=sum(presup,na.rm = TRUE),ejec=sum(presupuesto_ejecutado,na.rm = TRUE))
```

Row {data-height=350}
-----------------------------------------------------------------------

### Numero de Proyectos por financiamiento

```{r}
num_proy <- financiamiento %>% 
  group_by(tipo_financiamiento) %>%
  summarise(Cantidad = sum(tot_proy)) %>% 
  collect()

proy_reg <- ggplot(num_proy, aes(x = tipo_financiamiento, y = Cantidad, fill = tipo_financiamiento)) +
  geom_bar(stat = "identity",position = "stack") +
  labs(title = "Proyectos por financiamiento", x = "Financiamiento", y = "Cantidad") +
  coord_flip()+
  theme_classic()
ggplotly(proy_reg)

```


### % ejecucion por financiamiento

```{r}
plot_ly(num_proy,labels=~tipo_financiamiento,values=~Cantidad,type = "pie") %>% 
  layout(title="Proyectos por financiamiento",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

```

Row {data-height=650, .tabset .tabset-fade}
-----------------------------------------------------------------------

### Barras 

```{r}
barra_reg <- ggplot(financiamiento, aes(x = gestion, y = presup + ejec, fill = tipo_financiamiento)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Presupuesto y Ejecución por financiamiento",
       x = "Gestión", y = "Ejecución") +
  theme_minimal()
ggplotly(barra_reg)
```

### Densidad 

```{r}
area_reg <- ggplot(financiamiento, aes(x = ejec)) +
  geom_density(aes(fill = tipo_financiamiento), alpha = 0.5) +
  labs(title = "Presupuesto y Ejecución por financiamiento",
       x = "Ejecución") +
  theme_minimal()
ggplotly(area_reg)
```

### Tabla 

```{r}
datatable(financiamiento %>% collect())
```

# Grafico Dinamico
```{r}

```


# Informe {data-icon="fa-file-alt"}

```{r, out.width='100%', out.height="100%" }
include_graphics("Informe.pdf")
```